package com.witframework.baseapp.security.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.witframework.baseapp.security.DataSetAuth;
import com.witframework.baseapp.security.DataSetAuthVo;
import com.witframework.baseapp.security.service.DataSetAuthService;
import com.witframework.core.WitFrameWork;
import com.witframework.core.logic.IBaseService;
import com.witframework.web.mvc.WebContext;

/**
 * 数据权限指定类别的已分配权限列表控制器类
 * @author wuxinyang
 * Jul 21, 2009 3:59:51 PM
 *
 */
public class DataSetAuthSetController{
	@Autowired
	private DataSetAuthService dataSetAuthService;
	public String executor(WebContext webContext) {			
		webContext.getParameterAndsetAttribute("data");		
		webContext.getParameterAndsetAttribute("properties");
		webContext.getParameterAndsetAttribute("category");
		webContext.getParameterAndsetAttribute("entryid");
		webContext.getParameterAndsetAttribute("entryname");		
		webContext.getParameterAndsetAttribute("datatype");
		webContext.getParameterAndsetAttribute("datastyle");
		webContext.getParameterAndsetAttribute("titles");		
		webContext.getParameterAndsetAttribute("multiple");
		webContext.getParameterAndsetAttribute("data2");		
		webContext.getParameterAndsetAttribute("root");
		webContext.getParameterAndsetAttribute("parentid");
		webContext.getParameterAndsetAttribute("nodeid");
		webContext.getParameterAndsetAttribute("nodeid2");
		webContext.getParameterAndsetAttribute("nodelabel");
		webContext.getParameterAndsetAttribute("nodelabel2");
		webContext.getParameterAndsetAttribute("onlyLeaf");
		webContext.getParameterAndsetAttribute("height");
		webContext.getParameterAndsetAttribute("width");
		webContext.getParameterAndsetAttribute("target");
		String target_value1=webContext.getParameterAndsetAttribute("target_value1");
		if(StringUtils.isEmpty(target_value1))
			webContext.getParameterAndsetAttribute("target_value");
		else
			webContext.setAttribute("target_value",target_value1);
		List<DataSetAuthVo> dataSetAuthList = dataSetAuthService.getAllAuth(webContext.getFilter(),webContext.getParameterAndsetAttribute("category"));
		webContext.setAttribute("dataSetAuthList",dataSetAuthList);		
		return "classpath:/com/witframework/webroot/framework/security/datasetauth.jsp";
	}
	@Transactional
	public String store(WebContext webContext) {
		IBaseService service=WitFrameWork.getSimpleBaseService(DataSetAuth.class);
		String category=webContext.getParameter("category");
		String users=webContext.getParameter("user_name");		
		String selectedItems=webContext.getParameter("selectedItems");
		String target=webContext.getParameter("target_value1");
		String target_value=webContext.getParameter("target_value");
		String[] targerItems=null;
		if(StringUtils.isNotEmpty(target_value)){
			targerItems=StringUtils.split(target_value,",");			
		}else if(StringUtils.isNotEmpty(target)){
			targerItems=StringUtils.split(target,",");
		}
		if(targerItems==null)
			targerItems=new String[]{"|"};
		String[] usernames=StringUtils.split(users, ",");
		String[] userids=StringUtils.split(webContext.getParameter("user_id"), ",");
		String[] auths=StringUtils.split(selectedItems,";");
		for(int i=0;i<usernames.length;i++){
			for(String auth:auths){
				for(String targetItem:targerItems){
					String[] targetObject=StringUtils.split(targetItem,"|");
					String[] authItem=StringUtils.split(auth,"|");
					DataSetAuth dsa=new DataSetAuth();
					dsa.setUsername(usernames[i]);
					dsa.setUser_id(userids[i]);
					dsa.setCategory(category);
					dsa.setIdentifier(authItem[0]);
					dsa.setName(authItem[1]);
					Map filter=new HashMap();
					filter.put("username", dsa.getUsername());
					filter.put("identifier", dsa.getIdentifier());
					if(targetObject.length==2){
						filter.put("target", targetObject[0]);						
						dsa.setTarget(targetObject[0]);
						dsa.setTargetname(targetObject[1]);
					}else if(targetObject.length==1){
						filter.put("target", targetObject[0]);
						dsa.setTarget(targetObject[0]);
					}
					if(service.findBy(filter).size()==0){
						service.saveOrUpdate(dsa);
						//WitCacheFactory.getCache("dataSetAuth").store(dsa);
					}
				}
			}
		}		
		return executor(webContext);
	}
	@Transactional
	public String delete(WebContext webContext){
		IBaseService service=WitFrameWork.getSimpleBaseService(DataSetAuth.class);
		String[] items=webContext.getSelItemList();
		for(String id:items){
			DataSetAuth ds=(DataSetAuth)service.get(id);
			Map filter=new HashMap();
			filter.put("username",ds.getUsername());
			if(StringUtils.isNotEmpty(ds.getTarget())){
				filter.put("target", ds.getTarget());
			}
			service.deleteAll(service.findBy(filter));
		}
		return executor(webContext);
	}
	public String edit(WebContext webContext){
		return executor(webContext);
	}
}
